<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\MomentCommentOp;
use App\Http\Controllers\Controller;
use App\Model\Message;
use App\Model\Moment;
use App\Model\MomentComment;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class MomentCommentController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('动态留言管理');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new MomentComment());
        $grid->model()->with('moment.userProfile')->orderBy('id', 'desc');
        $grid->created_at('留言时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->moment()->id('动态ID');
        $grid->moment()->words('动态文字');
        $grid->moment()->user_profile_id('动态发布用户ID')->display(function ($data) {
            return json_decode($this->moment, true)['user_profile']['user_profile_id'];
        });
        $grid->moment()->nick_name('动态发布用户昵称')->display(function ($data) {
            return json_decode($this->moment, true)['user_profile']['nick_name'];
        });
        $grid->id('留言ID');
        $grid->userProfile()->user_profile_id('留言用户ID');
        $grid->userProfile()->nick_name('留言用户昵称');
        $grid->userProfile()->gender('留言性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->content('留言内容');
        // $grid->moment()->images('关联动态图片')->display(function ($data) {
        //     if (!$data) return '-';
        //     return ComUtil::buidlImgaeHtml($data);
        // });
        $grid->flag('状态')->display(function ($data) {
            return Moment::STATUS[$data] ?? '未知状态';
        });

        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->equal('userProfile.user_profile_id', '留言用户ID');
            $filter->equal('flag', '留言状态')->select(Moment::STATUS);
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new MomentCommentOp(
                $actions->getKey(),
                $actions->row,
                'comment/verify'
            ));
        });
        return $grid;
    }

    function verify()
    {
        $id = request('id');
        $status = request('status');
        $action = request('action');
        $record = MomentComment::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //站内通知
        // $pubTime = (string)$record->created_at;
        // $systemMsg = "很抱歉，您于{$pubTime}发表的动态评论涉嫌违规，已被系统删除～";
        // Message::pushSystem($record->user_id, $systemMsg);
        if ($action == 'del_all') {
            MomentComment::where('user_id', $record->user_id)->update(['flag' => MomentComment::STATUS_DEL]);
        } else {
            //修改状态
            $record->update(['flag' => $status]);
        }
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
